package com.example.gw_back_end.controller;

import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.domain.entities.Category;
import com.example.gw_back_end.domain.entities.Game;
import com.example.gw_back_end.service.GameService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("game")
public class GameController {

    private final GameService gameService;
    public GameController(GameService gameService) {
        this.gameService = gameService;
    }

    // 获取游戏列表
    @GetMapping("get_game_list")
    public Result getGameList(@ModelAttribute PageParam pageParam,
                              @RequestParam(required = false) List<Integer> categoryIdList,
                              @RequestHeader(value = "X-Client-Type") String clientType) {
        return Result.success(gameService.getGameList(pageParam,categoryIdList,clientType));
    }

    // 游戏新增
    @PostMapping("add_game")
    public Result addGame(@RequestBody Game game) {
        if(gameService.addGame(game)){
            return Result.success();
        } else {
            return Result.error("新增游戏失败,请稍后重试！");
        }
    }

    // 删除游戏
    @DeleteMapping("delete_game")
    public Result deleteGame(Integer id) {
        if(gameService.deleteGame(id)){
            return Result.success();
        } else {
            return Result.error("删除游戏失败,请稍后重试！");
        }
    }

    // 获取游戏
    @GetMapping("get_game")
    public Result getGame(Integer id) {
        return Result.success(gameService.getGameById(id));
    }

    // 更新游戏
    @PutMapping("update_game")
    public Result updateGame(@RequestBody Game game) {
        if(gameService.updateGame(game)){
            return Result.success();
        } else {
            return Result.error("更新游戏失败,请稍后重试！");
        }
    }
}
